package com.beiding.sqlbuilder.impl;

import com.beiding.sqlbuilder.*;

import java.util.ArrayList;
import java.util.List;

public class SimpleConnectorConditionUnitContainer extends SimpleConditionUnitContainer implements ConnectorConditionUnitContainer {

    private LogicType connectorType;

    public SimpleConnectorConditionUnitContainer(LogicType connectorType) {
        super();
        this.connectorType = connectorType;
    }

    public SimpleConnectorConditionUnitContainer(LogicType connectorType, List<ConditionUnit> conditionUnits) {
        super(conditionUnits);
        this.connectorType = connectorType;
    }

    public SimpleConnectorConditionUnitContainer(LogicType connectorType, ConditionUnit... conditionUnits) {
        super(conditionUnits);
        this.connectorType = connectorType;
    }

    @Override
    public LogicType getConnectorType() {
        return connectorType;
    }


    private void dealConnector() {
        ConditionUtils.dealConnector(this.conditionUnits, this.connectorType);
    }

    @Override
    public void build(BuilderContext builderContext) {
        if (conditionUnitHolders == null) {
            dealConnector();
            if (conditionUnits.size() == 0) {
                throw new RuntimeException("条件容器中至少包含一个条件");
            }
            conditionUnitHolders = new ArrayList<>();
            for (ConditionUnit conditionUnit : this.conditionUnits) {
                conditionUnit.build(builderContext);
                conditionUnitHolders.add(ConditionUtils.buildConditionUnitHolder(conditionUnit, builderContext.getConditionUnitHandlers()));
            }
        }
    }
}
